package com.uniontech.curd.dao;

import com.mysql.cj.util.StringUtils;
import com.uniontech.curd.entity.Users;
import com.uniontech.curd.entity.http.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class CURDDao {
    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    public boolean add (Users users){
        String sql = null;
        if(!StringUtils.isNullOrEmpty(users.getName())){
            sql = "INSERT INTO users(id, name, age, tel) " +
            "VALUES (:id, :name,:age,:tel)";
        }
        SqlParameterSource ps = new BeanPropertySqlParameterSource(users);
        int count = jdbcTemplate.update(sql, ps);
        if (count > 0){
            return true;
        } else {
            return false;
        }
    }

    public boolean update (Users users){
        String sql = null;
        if(!StringUtils.isNullOrEmpty(users.getName())){
            sql = "update users set name=:name, age=:age, tel=:tel where id=:id";
        }
        SqlParameterSource ps = new BeanPropertySqlParameterSource(users);
        int count = jdbcTemplate.update(sql, ps);
        if (count > 0){
            return true;
        } else {
            return false;
        }
    }

    public boolean delete (Users users){
        String sql = null;
        if(users.getId() != null){
            sql = "delete from users where id=:id";
        }
        SqlParameterSource ps = new BeanPropertySqlParameterSource(users);
        int count = jdbcTemplate.update(sql, ps);
        if (count > 0){
            return true;
        } else {
            return false;
        }
    }


    public int count(String searchContent) {
        Map<String, Object> map = new HashMap<>();
        String sql = "select count(1) from users";
        if(!StringUtils.isNullOrEmpty(searchContent)){
            sql +=
 " where id like :searchContent or name like :searchContent or age like :searchContent or tel like :searchContent";
            map.put("searchContent", "%"+searchContent+"%");
        }
        SqlParameterSource ps = new MapSqlParameterSource(map);
        return jdbcTemplate.queryForObject(sql, ps, Integer.class);
    }

    public List<Users> list(String searchContent) {
        Map<String, Object> map = new HashMap<>();
        String sql = "select * from users";
        if(!StringUtils.isNullOrEmpty(searchContent)){
            sql +=
" where id like :searchContent or name like :searchContent or age like :searchContent or tel like :searchContent";
        }
        map.put("searchContent", "%"+searchContent+"%");
        SqlParameterSource ps = new MapSqlParameterSource(map);
        return jdbcTemplate.query(sql, ps, new BeanPropertyRowMapper<Users>(Users.class));
    }

}
